package com.example.demo.modular.service;

import com.example.demo.util.Token;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liukai
 * @date 2020/12/21 0021 - 12:21
 */
@Service
public class BettService {

    public String beeting(String bettingIssue,String bettingNumber,int money){
        List<Map<String,Object>> bettingData = new ArrayList<>();
//        String s = restTemplate.postForObject("https://m.861656.com/v1/betting/addBetting", bettingData, String.class);
        //headers
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("X-Token", Token.token);
        requestHeaders.add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");
//        requestHeaders.add("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
        //body
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("lotteryCode", "1407");
        map.put("playDetailCode", "1407A10");
        map.put("bettingNumber", bettingNumber);
        map.put("bettingCount", 1);
        map.put("bettingAmount", money);
        map.put("bettingPoint", 8.5);
        map.put("bettingUnit", 1);
        map.put("bettingIssue", bettingIssue);
        map.put("graduationCount", 1);
        bettingData.add(map);
        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
        requestBody.add("bettingData", bettingData);
//        bettingData.add(requestBody);
        //HttpEntity
        HttpEntity<MultiValueMap> requestEntity = new HttpEntity<>(requestBody, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
        //post
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("https://m.861656.com/v1/betting/addBetting", requestEntity, String.class);
        System.out.println(responseEntity.getBody());
        return responseEntity.getBody();
    }
}
